package com.dkd.sbtz.domain.ordervo.exportsheet;

// src/main/java/com/dkd/sbtz/service/export/ExportAssembler.java

import com.dkd.sbtz.domain.AssetOrderAttachment;
import com.dkd.sbtz.domain.AssetWorkOrder;
import com.dkd.sbtz.domain.ordervo.exportsheet.OrderAttachmentRow;
import com.dkd.sbtz.domain.ordervo.exportsheet.OrderMainRow;
import com.dkd.sbtz.domain.ordervo.exportsheet.OrderSpareRow;
import org.apache.poi.ss.usermodel.Row;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExportAssembler {

    private static final SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd");

    private static String fmt(java.util.Date d){
        return d == null ? "" : DF.format(d);
    }
    private static String nz(Object o){ return o == null ? "" : String.valueOf(o); }



    private static final DateTimeFormatter DTF =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    // 适配 LocalDateTime
    private static String fmtLocalTime(LocalDateTime v) {
        return v == null ? "" : v.format(DTF);
    }

    // 如仍有旧代码使用 Date，可保留这个重载
    private static String fmtDate(Date v) {
        if (v == null) return "";
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v);
    }
    /** ---- 数据 -> 行模型 ---- */
    public static List<OrderMainRow> toMainRows(List<AssetWorkOrder> list){
        List<OrderMainRow> out = new ArrayList<>();
        for (AssetWorkOrder o : list) {
            OrderMainRow r = new OrderMainRow();
            r.id            = o.getId();
            r.woCode        = o.getWoCode();
            r.title         = o.getTitle();
            r.orderType     = o.getOrderType();
            r.urgency       = o.getUrgency();
            r.status        = o.getStatus();
            r.source        = o.getSource();
            r.requesterId   = o.getRequesterId();
            r.dispatcherId  = o.getDispatcherId();
            r.receiverId    = o.getReceiverId();
            r.submitTime    = fmtLocalTime(o.getSubmitTime());
            r.createTime    = fmt(o.getCreateTime());
            r.planStartTime = fmt(o.getPlanStartTime());
            r.planEndTime   = fmt(o.getPlanEndTime());
            r.actualStartTime = fmtLocalTime(o.getActualStartTime());
            r.actualEndTime   = fmtLocalTime(o.getActualEndTime());
            r.priorityScore = o.getPriorityScore();

            if (o.getNode() != null){
                r.equipmentName  = nz(o.getNode().getName());
                r.equipmentCode  = nz(o.getNode().getCode());
                r.equipmentModel = nz(o.getNode().getModel());
                r.equipmentSpec  = nz(o.getNode().getSpecification());
                r.equipmentBrand = nz(o.getNode().getBrand());
            }
            r.faultDesc = nz(o.getFaultDesc());
            out.add(r);
        }
        return out;
    }

    public static List<OrderAttachmentRow> toAttachmentRows(List<AssetWorkOrder> list){
        List<OrderAttachmentRow> out = new ArrayList<>();
        for (AssetWorkOrder o : list) {
            if (o.getAttachments() == null) continue;
            for (AssetOrderAttachment a : o.getAttachments()) {
                OrderAttachmentRow r = new OrderAttachmentRow();
                r.orderId    = o.getId();
                r.fileName   = a.getFileName();
                r.fileType   = a.getFileType();
                r.fileSize   = a.getFileSize();
                r.uploadTime = nz(a.getUploadTime());
                r.uploader   = a.getUploader();
                r.url        = a.getFileUrl(); // 已建议在 service 里转绝对URL
                out.add(r);
            }
        }
        return out;
    }

    public static List<OrderSpareRow> toSpareRows(List<AssetWorkOrder> list){
        List<OrderSpareRow> out = new ArrayList<>();
        for (AssetWorkOrder o : list) {
            if (o.getSpareInfos() == null) continue;
            o.getSpareInfos().forEach(s -> {
                OrderSpareRow r = new OrderSpareRow();
                r.orderId        = o.getId();
                r.code           = s.getCode();
                r.name           = s.getName();
                r.qty            = s.getQty();
                r.stockTotal     = s.getStockTotal();
                r.stockAvailable = s.getStockAvailable();
                out.add(r);
            });
        }
        return out;
    }

    /** ---- 写入一行（给 ExcelSheets 调用） ---- */
    public static void fillMainRow(Row row, OrderMainRow d){
        int c = 0;
        row.createCell(c++).setCellValue(nz(d.id));
        row.createCell(c++).setCellValue(nz(d.woCode));
        row.createCell(c++).setCellValue(nz(d.title));
        row.createCell(c++).setCellValue(nz(d.orderType));
        row.createCell(c++).setCellValue(d.urgency == null ? "" : String.valueOf(d.urgency));
        row.createCell(c++).setCellValue(nz(d.status));
        row.createCell(c++).setCellValue(nz(d.source));
        row.createCell(c++).setCellValue(nz(d.requesterId));
        row.createCell(c++).setCellValue(nz(d.dispatcherId));
        row.createCell(c++).setCellValue(nz(d.receiverId));
        row.createCell(c++).setCellValue(d.submitTime);
        row.createCell(c++).setCellValue(d.createTime);
        row.createCell(c++).setCellValue(d.planStartTime);
        row.createCell(c++).setCellValue(d.planEndTime);
        row.createCell(c++).setCellValue(d.actualStartTime);
        row.createCell(c++).setCellValue(d.actualEndTime);
        row.createCell(c++).setCellValue(d.priorityScore == null ? "" : String.valueOf(d.priorityScore));
        row.createCell(c++).setCellValue(nz(d.equipmentName));
        row.createCell(c++).setCellValue(nz(d.equipmentCode));
        row.createCell(c++).setCellValue(nz(d.equipmentModel));
        row.createCell(c++).setCellValue(nz(d.equipmentSpec));
        row.createCell(c++).setCellValue(nz(d.equipmentBrand));
        row.createCell(c++).setCellValue(nz(d.faultDesc));
    }

    public static void fillAttachmentRow(Row row, OrderAttachmentRow d){
        int c = 0;
        row.createCell(c++).setCellValue(nz(d.orderId));
        row.createCell(c++).setCellValue(nz(d.fileName));
        row.createCell(c++).setCellValue(nz(d.fileType));
        row.createCell(c++).setCellValue(d.fileSize == null ? "" : String.valueOf(d.fileSize));
        row.createCell(c++).setCellValue(nz(d.uploadTime));
        row.createCell(c++).setCellValue(nz(d.uploader));
        row.createCell(c++).setCellValue(nz(d.url));
    }

    public static void fillSpareRow(Row row, OrderSpareRow d){
        int c = 0;
        row.createCell(c++).setCellValue(nz(d.orderId));
        row.createCell(c++).setCellValue(nz(d.code));
        row.createCell(c++).setCellValue(nz(d.name));
        row.createCell(c++).setCellValue(d.qty == null ? "" : String.valueOf(d.qty));
        row.createCell(c++).setCellValue(nz(d.stockTotal));
        row.createCell(c++).setCellValue(nz(d.stockAvailable));
    }
}
